<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/13
 * Time: 14:39
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Casesuser extends Base
{
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where = [
                'case_jianqie' => 1
            ];
            if(isset($username) && $username != ''){
                $where['user_username|user_phone'] = ['like', '%' . $username . '%'];
            }
            if(isset($case_see_uids) && $case_see_uids != ''){
                $where['case_see_uids'] = ['like', '%,' . $case_see_uids . ',%'];
            }
            if(isset($user_name) && $user_name != ''){
                $where['case_name|case_address|case_info'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($user_name2) && $user_name2 != ''){
                $where['case_username|case_username2|case_username3|case_phone|case_phone2|case_phone3'] = ['like', '%' . $user_name2 . '%'];
            }
            if(isset($user_name3) && $user_name3 != ''){
                $where['case_excel|case_excel_phone'] = ['like', '%' . $user_name3 . '%'];
            }

            if(isset($room) && $room != ''){
                $where['case_room'] = $room;
            }
            if(isset($style) && $style != ''){
                $where['case_style'] = $style;
            }
            if(isset($size) && $size != ''){
                $sizeData = Db::table('up_size')->where('size_id',$size)->find();
                $where['case_size'] = ['between',"{$sizeData['size_min']},{$sizeData['size_max']}"];
            }
            if(isset($s_qu) && $s_qu != ''){
                $where['case_qu'] = $s_qu;
            }
            if(isset($case_done) && $case_done != ''){
                $where['case_done'] = $case_done;
            }
            if(isset($case_jianqie) && $case_jianqie != ''){
                $where['case_jianqie'] = $case_jianqie;
            }
            if(isset($indexindex) && $indexindex != "")
            {
                $where['case_index'] = $indexindex;
            }
            if(isset($case_jindu) && $case_jindu != "")
            {
                $where['case_jindu'] = $case_jindu;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_case c')
                ->join('up_area a', 'c.case_qu = a.district_id', 'left')
                ->join('up_room r', 'c.case_room = r.room_id', 'left')
                ->join('up_style s', 'c.case_style = s.style_id', 'left')
                ->join('up_u_user u', 'c.case_who = u.user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $field = 'c.*,district,room_name,style_name,user_username,user_phone';
            $lists = Db::table('up_case c')
                ->join('up_area a', 'c.case_qu = a.district_id', 'left')
                ->join('up_room r', 'c.case_room = r.room_id', 'left')
                ->join('up_style s', 'c.case_style = s.style_id', 'left')
                ->join('up_u_user u', 'c.case_who = u.user_id', 'left')
                ->field($field)
                ->where($where)
                ->page($NowPage,$limits)
                ->order('case_sort desc,case_time desc')
                ->select();
            if($lists){
                foreach ($lists as $k=>$v){
                    $lists[$k]['jindu_name'] = '无';
                    $lists[$k]['who'] = '后台';
                    $lists[$k]['names'] = '';
                    if(trim($v['case_excel_uids'],',')){
                        $ids = trim($v['case_excel_uids'],',');
                        $names = Db::table('up_u_user')->where('user_id','in',$ids)->column('user_username');
                        $lists[$k]['names'] = implode($names,',');
                    }
                    if($v['case_jindu']) $lists[$k]['jindu_name'] = Db::table('up_jindu')->where('jindu_id',$v['case_jindu'])->value('jindu_name');
                    if($v['case_who'] > 0) $lists[$k]['who'] = $v['user_username'] . '(' . $v['user_phone'] . ')';
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $res = $this->getoptions();
        $this->assign('qus',$res[0]);
        $this->assign('room',$res[1]);
        $this->assign('style',$res[2]);
        $this->assign('size',$res[3]);
        $this->assign('jindu',$res[5]);
        $pps = Db::table('up_u_user')
            ->where('user_status',1)
            ->where('user_type','like','%2%')
            ->field('user_id,user_d_name,user_username,user_d_company,user_phone')
            ->order('user_letter asc')
            ->select();
        $this->assign('pps',$pps);
        return $this->fetch();
    }

    public function getoptions()
    {
        $qus = Db::table('up_area')
            ->where('pid',108)
            ->field('district_id,district')
            ->select();
        $room = Db::table('up_room')
            ->field('room_id,room_name')
            ->order('room_sort desc')
            ->select();
        $style = Db::table('up_style')
            ->field('style_id,style_name')
            ->order('style_sort desc')
            ->select();
        $size = Db::table('up_size')
            ->field('size_id,size_name')
            ->order('size_id asc')
            ->select();
        $users = Db::table('up_u_user')
            ->where('user_type','like','%3%')
            ->field('user_id,user_username,user_phone')
            ->select();
        $jindu = Db::table('up_jindu')
            ->where('jindu_status',1)
            ->field('jindu_id,jindu_name')
            ->order('jindu_sort desc')
            ->select();
        $res = [$qus,$room,$style,$size,$users,$jindu];
        return $res;
    }
    public function getDatas()
    {
        $pp_id = input('param.pp_id');
        $data = Db::table('up_store')
            ->where('s_pp_id', $pp_id)
            ->where('s_status', 1)
            ->field('s_id,s_name')
            ->order('s_sort desc')
            ->select();
        return json(['code'=>200,'msg'=>$data]);
    }
    public function getDatas2()
    {
        $pp_id = input('param.pp_id');
        $data = Db::table('up_store')
            ->where('s_pp_id', $pp_id)
            ->where('s_status', 1)
            ->field('s_id,s_name')
            ->order('s_sort desc')
            ->select();
        $data1 = Db::table('up_store_types')
            ->where('st_pp_id', $pp_id)
            ->where('st_status', 1)
            ->order('st_sort desc')
            ->select();
        $data2 = Db::table('up_store_pps')
            ->where('spp_pp_id', $pp_id)
            ->where('spp_status', 1)
            ->order('spp_sort desc')
            ->select();
        $data3 = Db::table('up_store_style')
            ->where('sstyle_pp_id', $pp_id)
            ->where('sstyle_status', 1)
            ->order('sstyle_sort desc')
            ->select();

        return json(['code'=>200,'msg'=>$data,'msg1'=>$data1,'msg2'=>$data2,'msg3'=>$data3]);
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function newAdd(){
        if(Request::instance()->isPost()){
            extract(input());
//            if($case_uids) {
//                Db::table('up_u_user')->where('user_id','in',$case_uids)->update(['user_order_time' => date('Y-m-d H:i:s')]);
//                $case_uids = ','.$case_uids.',';
//            }
            $data = [
                'case_name' => $case_name,
                'case_qu' => $case_qu,
                'case_address' => $case_address,
//                'case_banner' => $case_banner,
//                'case_video' => $case_video,
                'case_two_pic' => $case_two_pic,
//                'case_imgs' => $case_imgs,
//                'case_photo' => $case_photo,
                'case_info' => $case_info,
                'case_jindu' => $case_jindu,
                'case_username' => $case_username,
                'case_username2' => $case_username2,
                'case_username3' => $case_username3,
                'case_username4' => $case_username4,
                'case_username5' => $case_username5,
                'case_phone' => $case_phone,
                'case_phone2' => $case_phone2,
                'case_phone3' => $case_phone3,
                'case_phone4' => $case_phone4,
                'case_phone5' => $case_phone5,
                'case_company' => $case_company,
                'case_shejishi' => $case_shejishi,
//                'case_uids' => $case_uids,
                'case_excel' => $case_excel,
                'case_excel_phone' => $case_excel_phone,
                'case_excel_uids' => ','.$case_excel_uids.',',
                'case_room' => $case_room,
                'case_style' => $case_style,
                'case_size' => $case_size,
                'case_sort' => $case_sort,
                'case_company_letter' => '',
                'case_shejishi_letter' => '',
                'case_shejishi_have' => 1,
                'case_time' => date('Y-m-d H:i:s')
            ];
            $lonlat = explode(',',$lonlat);
            $data['case_lat'] = $lonlat[0];
            $data['case_lon'] = $lonlat[1];
            $str = trim($case_name,' ');
            $str= iconv("UTF-8","gb2312//IGNORE", $str);
            $tmp = bin2hex(substr($str,0,1));
            if($tmp>='B0'){ //汉字的开始
                $t=$this->getLetter(hexdec(bin2hex(substr($str,0,2))));
                $letter = chr($t);
            }else{
                $letter = strtoupper(substr($str,0,1));
            }
            $letter = $this->getLetter2($letter,mb_substr(trim($case_name,' '),0,1));
            $data['case_letter'] = $letter;
//            if($seeids == 1){
//                $data['case_see_uids'] = -1;
//            }else{
//                if(!isset($case_see_uids) || !$case_see_uids) return json(['code' => 100, 'msg' => '请选择商家']);
//                $data['case_see_uids'] = ',' . $case_see_uids . ',';
//            }
            //--------------------------字母------------------------------------------
            if($case_company){
                $letter = $this->userLetter(trim($case_company,' '));
                $data['case_company_letter'] = $letter;
            }
            if($case_shejishi){
                $letter = $this->userLetter(trim($case_shejishi,' '));
                $data['case_shejishi_letter'] = $letter;
                $data['case_shejishi_have'] = 2;
            }
            $res = Db::table('up_case')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $res = $this->getoptions();
        $this->assign('qus',$res[0]);
        $this->assign('room',$res[1]);
        $this->assign('style',$res[2]);
        $this->assign('size',$res[3]);
        $this->assign('users',$res[4]);
        $this->assign('jindu',$res[5]);
        $pps = Db::table('up_u_user')
            ->where('user_status',1)
            ->where('user_type','like','%2%')
            ->field('user_id,user_d_name,user_username,user_d_company,user_phone')
            ->order('user_letter asc')
            ->select();
        $this->assign('pps',$pps);
        return $this->fetch('casesuser/newadd');
    }
    /**
     * 编辑
     * @return mixed
     */
    public function newEdit(){
        if(Request::instance()->isPost()){
            extract(input());
//            if($case_uids) {
//                Db::table('up_u_user')->where('user_id','in',$case_uids)->update(['user_order_time' => date('Y-m-d H:i:s')]);
//                $case_uids = ','.$case_uids.',';
//            }
            $data = [
                'case_name' => $case_name,
                'case_qu' => $case_qu,
                'case_address' => $case_address,
//                'case_banner' => $case_banner,
//                'case_video' => $case_video,
                'case_two_pic' => $case_two_pic,
//                'case_imgs' => $case_imgs,
//                'case_photo' => $case_photo,
                'case_info' => $case_info,
                'case_jindu' => $case_jindu,
                'case_username' => $case_username,
                'case_username2' => $case_username2,
                'case_username3' => $case_username3,
                'case_username4' => $case_username4,
                'case_username5' => $case_username5,
                'case_phone' => $case_phone,
                'case_phone2' => $case_phone2,
                'case_phone3' => $case_phone3,
                'case_phone4' => $case_phone4,
                'case_phone5' => $case_phone3,
                'case_company' => $case_company,
                'case_shejishi' => $case_shejishi,
//                'case_uids' => $case_uids,
                'case_excel' => $case_excel,
                'case_excel_phone' => $case_excel_phone,
                'case_excel_uids' => ','.$case_excel_uids.',',
                'case_room' => $case_room,
                'case_style' => $case_style,
                'case_size' => $case_size,
                'case_sort' => $case_sort,
                'case_company_letter' => '',
                'case_shejishi_letter' => '',
                'case_shejishi_have' => 1,
//                'case_time' => date('Y-m-d H:i:s')
            ];
            $lonlat = explode(',',$lonlat);
            $data['case_lat'] = $lonlat[0];
            $data['case_lon'] = $lonlat[1];
            $str = trim($case_name,' ');
            $str= iconv("UTF-8","gb2312//IGNORE", $str);
            $tmp = bin2hex(substr($str,0,1));
            if($tmp>='B0'){ //汉字的开始
                $t=$this->getLetter(hexdec(bin2hex(substr($str,0,2))));
                $letter = chr($t);
            }else{
                $letter = strtoupper(substr($str,0,1));
            }
            $letter = $this->getLetter2($letter,mb_substr(trim($case_name,' '),0,1));
            $data['case_letter'] = $letter;
//            if($seeids == 1){
//                $data['case_see_uids'] = -1;
//            }else{
//                if(!isset($case_see_uids) || !$case_see_uids) return json(['code' => 100, 'msg' => '请选择商家']);
//                $data['case_see_uids'] = ',' . $case_see_uids . ',';
//            }
            //--------------------------字母------------------------------------------
            if($case_company){
                $letter = $this->userLetter(trim($case_company,' '));
                $data['case_company_letter'] = $letter;
            }
            if($case_shejishi){
                $letter = $this->userLetter(trim($case_shejishi,' '));
                $data['case_shejishi_letter'] = $letter;
                $data['case_shejishi_have'] = 2;
            }
            $res = Db::table('up_case')->where('case_id',$case_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $case_id = input('case_id');
        $info = Db::name('case')
            ->where('case_id',$case_id)
            ->find();
        if($info['case_see_uids'] == -1) {
            $sel = 1;
        }else{
            $sel = 2;
        }
        $info['case_see_uids'] = explode(',',trim($info['case_see_uids'],','));
        $info['case_excel_uids'] = explode(',',trim($info['case_excel_uids'],','));
        $info['case_uids'] = explode(',',trim($info['case_uids'],','));
        $res = $this->getoptions();
        $this->assign('qus',$res[0]);
        $this->assign('room',$res[1]);
        $this->assign('style',$res[2]);
        $this->assign('size',$res[3]);
        $this->assign('users',$res[4]);
        $this->assign('jindu',$res[5]);
        $this->assign('info',$info);
        $this->assign('sel',$sel);
        $pps = Db::table('up_u_user')
            ->where('user_status',1)
            ->where('user_type','like','%2%')
            ->field('user_id,user_d_name,user_username,user_d_company,user_phone')
            ->order('user_letter asc')
            ->select();
        $this->assign('pps',$pps);
        return $this->fetch('casesuser/newedit');
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function newDel(){
        $new_id = input('id');
        $res = Db::table('up_case')->where('case_id',$new_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function orderDel(){
        $new_id = input('id');
        $res = Db::table('up_case_info')->where('info_id',$new_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }

    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        $id = input('id');
        $param = [
            'case_sort'=>input('value'),
        ];
        $res = Db::table('up_case')->where('case_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 案例订单排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateOrderSort(){
        $id = input('id');
        $param = [
            'info_sort'=>input('value'),
        ];
        $res = Db::table('up_case_info')->where('info_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 案例订单
     */
    public function caseOrder(){
        if(request()->isAjax()){
            extract(input());
            $where = [
                'info_case_id' => session('case_id')
            ];
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_case_info info')
                ->join('up_types t', 'info.info_t_id = t.t_id', 'left')
                ->join('up_pp pp', 'info.info_pp_id = pp.pp_id', 'left')
                ->join('up_store s', 'info.info_s_id = s.s_id', 'left')
                ->join('up_place p', 'info.info_p_id = p.p_id', 'left')
                ->join('up_u_user u', 'info.info_user_id = u.user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $field = 'info.*,t_name,pp_name,s_name,p_name,user_d_name';
            $lists = Db::table('up_case_info info')
                ->join('up_types t', 'info.info_t_id = t.t_id', 'left')
                ->join('up_pp pp', 'info.info_pp_id = pp.pp_id', 'left')
                ->join('up_store s', 'info.info_s_id = s.s_id', 'left')
                ->join('up_place p', 'info.info_p_id = p.p_id', 'left')
                ->join('up_u_user u', 'info.info_user_id = u.user_id', 'left')
                ->field($field)
                ->where($where)
                ->page($NowPage,$limits)
                ->order('info_sort desc,info_time desc')
                ->select();
            if($lists){
                foreach ($lists as $k=>$v){
                    $lists[$k]['typename'] = '品牌订单';
                    if($v['info_type'] == 2){
                        $lists[$k]['typename'] = '设计师订单';
                    }
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $case_id = input('case_id');
        session('case_id',$case_id);
        return $this->fetch('casesuser/order');
    }
    /**
     * 订单添加
     * @return mixed|\think\response\Json
     */
    public function orderAdd(){
        if(Request::instance()->isPost()){
            extract(input());
            $data1 = [
                'info_case_id' => session('case_id'),
                'info_info' => $info_info,
                'info_img' => $info_img,
                'info_type' => $type,
                'info_money' => $info_money,
                'info_sort' => $info_sort,
                'info_time' => date('Y-m-d H:i:s')
            ];
            if($type == 1){
                $data2 = [
                    'info_t_id' => $info_t_id,
                    'info_pp_id' => $info_pp_id,
                    'info_p_id' => $info_p_id,
                    'info_s_id' => $info_s_id,
                    'info_designer' => $info_designer,
                ];
            }else{
                if($info_user_id) {
                    Db::table('up_u_user')->where('user_id','in',$info_user_id)->update(['user_order_time' => date('Y-m-d H:i:s')]);
                }
                $data2 = [
                    'info_user_id' => $info_user_id
                ];
            }
            $data = array_merge($data1,$data2);
            $res = Db::table('up_case_info')->insertGetId($data);
            if($res){
                $this->allMoneyCase(session('case_id'));
                $this->imgInfo($info_img,$res);
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $types = Db::table('up_types')
            ->field('t_id,t_name')
            ->order('t_sort desc')
            ->select();
        $qus = Db::table('up_area')
            ->where('pid',108)
            ->field('district_id,district')
            ->select();
        $pps = Db::table('up_pp')
            ->field('pp_id,pp_name')
            ->order('pp_sort desc')
            ->select();
        $ps = Db::table('up_place')
            ->field('p_id,p_name')
            ->order('p_sort desc')
            ->select();
        $sss = Db::table('up_store')
            ->field('s_address')
            ->order('s_sort desc')
            ->select();
        $this->assign('types',$types);
        $this->assign('qus',$qus);
        $this->assign('pps',$pps);
        $this->assign('ps',$ps);
        $this->assign('sss',$sss);
        $this->assign('type',input('type'));
        $users2 = Db::table('up_u_user')
            ->where('user_status',1)
            ->where('user_d_type',2)
            ->field('user_id,user_d_name,user_d_company,user_phone')
            ->select();
        $this->assign('users2',$users2);
        return $this->fetch('casesuser/order_add');
    }
    /**
     * 订单编辑
     * @return mixed|\think\response\Json
     */
    public function orderEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $data1 = [
                'info_info' => $info_info,
                'info_img' => $info_img,
                'info_money' => $info_money,
                'info_sort' => $info_sort,
                'info_time' => date('Y-m-d H:i:s')
            ];
            if($type == 1){
                $data2 = [
                    'info_t_id' => $info_t_id,
                    'info_pp_id' => $info_pp_id,
                    'info_p_id' => $info_p_id,
                    'info_s_id' => $info_s_id,
                    'info_designer' => $info_designer,
                ];
            }else{
                if($info_user_id) {
                    Db::table('up_u_user')->where('user_id','in',$info_user_id)->update(['user_order_time' => date('Y-m-d H:i:s')]);
                }
                $data2 = [
                    'info_user_id' => $info_user_id
                ];
            }
            $data = array_merge($data1,$data2);
            $res = Db::table('up_case_info')->where('info_id',$info_id)->update($data);
            if($res){
                $this->allMoneyCase(session('case_id'));
                $this->imgInfo($info_img,$info_id);
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $info_id = input('info_id');
        $info = Db::name('case_info')
            ->where('info_id',$info_id)
            ->find();
        $types = Db::table('up_types')
            ->field('t_id,t_name')
            ->order('t_sort desc')
            ->select();
        $qus = Db::table('up_area')
            ->where('pid',108)
            ->field('district_id,district')
            ->select();
        $pps = Db::table('up_pp')
            ->where('pp_t_id',$info['info_t_id'])
            ->field('pp_id,pp_name')
            ->order('pp_sort desc')
            ->select();
        $ps = Db::table('up_place')
            ->field('p_id,p_name')
            ->order('p_sort desc')
            ->select();
        $sss = Db::table('up_store')
            ->where('s_pp_id',$info['info_pp_id'])
            ->field('s_id,s_name')
            ->order('s_sort desc')
            ->select();
        $this->assign('types',$types);
        $this->assign('qus',$qus);
        $this->assign('pps',$pps);
        $this->assign('ps',$ps);
        $this->assign('sss',$sss);
        $this->assign('info',$info);
        $users2 = Db::table('up_u_user')
            ->where('user_status',1)
            ->where('user_d_type',2)
            ->field('user_id,user_d_name,user_d_company,user_phone')
            ->select();
        $this->assign('users2',$users2);
        return $this->fetch('casesuser/order_edit');
    }
    /**
     * 冻结启用
     * @return mixed|\think\response\Json
     */
    public function changeCaseStatus(){
        if(session('agid') == 10){
            return json(['code' => 100, 'msg' =>' 您无权冻结']);
        }
        $id = input('id');
        $param = [
            'case_status'=>input('num'),
        ];
        $res = Db::name('case')->where('case_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 变化
     * @return mixed|\think\response\Json
     */
    public function changeBianhuaStatus(){
        if(session('agid') == 10){
            return json(['code' => 100, 'msg' =>' 您无权操作']);
        }
        $id = input('id');
        $param = [
            'case_bianhua'=>input('num'),
        ];
        Db::table('up_case_bianhua')->where('cb_caseid',$id)->delete();
        $res = Db::name('case')->where('case_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 冻结启用
     * @return mixed|\think\response\Json
     */
    public function changeStatus(){
        if(session('agid') == 10){
            return json(['code' => 100, 'msg' =>' 您无权冻结']);
        }
        $id = input('id');
        $param = [
            'info_status'=>input('num'),
        ];
        $res = Db::name('case_info')->where('info_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    public function allMoneyCase($case_id){
        $money = Db::table('up_case_info')->where('info_case_id',$case_id)->sum('info_money');
        Db::table('up_case')->where('case_id',$case_id)->setField('case_money',$money);
        return true;
    }
    public function imgInfo($imgs,$info_id){
        Db::table('up_case_info_banner')->where('pb_pp_id',$info_id)->delete();
        $imgs = trim($imgs,',');
        if($imgs){
            $taday = date('Y-m-d H:i:s');
            $imgs = explode(',',$imgs);
            $data = [];
            foreach ($imgs as $k => $v){
                $data[] = [
                    'pb_pp_id' => $info_id,
                    'pb_url' => $v,
                    'pb_sort' => 1,
                    'pb_time' => $taday,
                ];
            }
            Db::table('up_case_info_banner')->insertAll($data);
        }
        return true;
    }
    /**
     * 推荐
     * @return \think\response\Json
     */
    public function changeMainStatus(){
        $id = input('id');
        $param = [
            'info_top'=>input('num'),
        ];
        $res = Db::table('up_case_info')->where('info_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    //-------------------------------------------------轮播图------------------------------------------------
    /**
     * 轮播图首页展示
     * @return mixed|\think\response\Json
     */
    public function newBanner(){
        if(request()->isAjax ()){
            extract(input());
            $where = [
                'pb_pp_id' => session('case_id')
            ];
            if(isset($keyword) && $keyword != ''){
                $where['pb_title|pb_title_up'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($pb_where) && $pb_where != "")
            {
                $where['pb_where'] = $pb_where;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('case_banner')->where($where)->count();// 获取总条数
            $lists = Db::name('case_banner')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('pb_where asc,pb_sort desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $case_id = input('case_id');
        session('case_id',$case_id);
        return $this->fetch('casesuser/banner');
    }
//    public function bannerAdd(){
//        if(Request::instance()->isPost()){
//            extract(input());
//            $data = [
//                'pb_title' => $pb_title,
//                'pb_pp_id' => session('pb_pp_id'),
//                'pb_type' => $pb_type,
//                'pb_sort' => $pb_sort,
//                'pb_where' => $pb_where,
//                'pb_time' => date('Y-m-d H:i:s')
//            ];
//            if($pb_type == 1){
//                $data['pb_url'] = $pb_url;
//            }else{
//                $data['pb_url'] = $video;
//            }
//            $res = Db::table('up_pp_banner')->insert($data);
//            if($res){
//                return json(['code' => 200, 'msg' => '添加成功']);
//            }else{
//                return json(['code' => 100, 'msg' => '添加失败']);
//            }
//        }
//        return $this->fetch('pp/banneradd');
//    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function bannerAdd(){
        if(Request::instance()->isPost()){
            extract(input());
            $cpd_content1 = trim($cpd_content1,', ');
            if(!$cpd_content1){
                return json(['code' => 100, 'msg' => '上传内容不能为空！']);
            }
            $cpd_content1 = explode(',',$cpd_content1);
            $data = [];
            foreach ($cpd_content1 as $k => $v){
                $mww = [
                    'pb_title' => $pb_title,
                    'pb_title_up' => $pb_title_up,
                    'pb_pp_id' => session('case_id'),
                    'pb_type' => $pb_type,
                    'pb_sort' => $pb_sort,
                    'pb_where' => $pb_where,
                    'pb_url' => $v,
                    'pb_time' => date('Y-m-d H:i:s')
                ];
                $data[] = $mww;
            }
            $res = Db::table('up_case_banner')->insertAll($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $type = input('type');
        $this->assign('type',$type);
        return $this->fetch('casesuser/banneradd');
    }
    /**
     * 编辑
     * @return mixed
     */
    public function bannerEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'pb_title' => $pb_title,
                'pb_title_up' => $pb_title_up,
                'pb_where' => $pb_where,
                'pb_time' => date('Y-m-d H:i:s')
            ];
            $pb_type = Db::table('up_case_banner')->where('pb_id',$pb_id)->value('pb_type');
            if($pb_type != 2){
                $data['pb_url'] = $pb_url;
            }else{
                $data['pb_url'] = $video;
            }
            $res = Db::table('up_case_banner')->where('pb_id',$pb_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $pb_id = input('pb_id');
        $info = Db::name('case_banner')
            ->where('pb_id',$pb_id)
            ->find();
        $this->assign([
            'banner' => $info,
        ]);
        return $this->fetch('casesuser/banneredit');
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function bannerDel(){
        $nt_id = input('id');
        $res = Db::table('up_case_banner')->where('pb_id',$nt_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 批量删除
     * @return \think\response\Json
     */
    public function batchDel(){
        $nt_id = input('ids');
        $res = Db::table('up_case_banner')->where('pb_id','in',$nt_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 设置图片
     * @return \think\response\Json
     */
    public function batchSetTwoPics(){
        $nt_id = input('ids');
        if(!$nt_id) return json(['code' => 100, 'msg' => '请选择数据！']);
        $nt_id = explode(',',trim($nt_id,','));
        if(count($nt_id) > 2) return json(['code' => 100, 'msg' => '最多只能选择两条！']);

        $have = Db::table('up_case_banner')
            ->where('pb_id','in',$nt_id)
            ->where('pb_type','in','2,3')
            ->find();
        if($have) return json(['code' => 100, 'msg' => '只能选择图片类型的！']);
        $data = Db::table('up_case_banner')
            ->where('pb_id','in',$nt_id)
            ->find();
        $pics = Db::table('up_case_banner')
            ->where('pb_id','in',$nt_id)
            ->column('pb_url');
        $pics = implode($pics,',');
        Db::table('up_case')
            ->where('case_id',$data['pb_pp_id'])
            ->setField('case_two_pic',$pics);
        return json(['code' => 200, 'msg' => '启用成功']);
    }
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateBannerSort(){
        $id = input('id');
        $param = [
            'pb_sort'=>input('value'),
        ];
        $res = Db::name('case_banner')->where('pb_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }

}